package com.tongguan.paypos.utils;

import android.support.annotation.NonNull;

import com.tongguan.paypos.common.AppConstants;
import com.tongguan.paypos.common.User;
import com.tongguan.paypos.entity.MoneyCount;
import com.tongguan.paypos.entity.PrintRepeat;
import com.tongguan.paypos.entity.SettleOrder;
import com.tongguan.paypos.entity.Total;
import com.tongguan.paypos.entity.TransInfo;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Joylive on 2016/12/12 .
 */

public class PrintUtils {

    public static final int ERR_PAPER = -601;//缺纸
    public static final int ERR_OVERHEAT = -602;//打印头过热
    public static final int ERR_PARA = -603;//参数错误
    public static final int ERR_NO_BLACKMAKER = -604;//黑标检测失败
    public static final int ERR_PARA_VAL = -26216;//参数值错误

    /**
     * 打印单笔订单
     */
    public static String printOnlineOrder(TransInfo transInfo, int printIndex, boolean isCoupon, @NonNull MoneyCount moneyCount, boolean isRepeat) throws Exception {
        StringBuilder scriptBuilderNeck = new StringBuilder();
        scriptBuilderNeck.append("------------------------------------------------ \n");
        if (printIndex == 0) {
            scriptBuilderNeck.append("商户存根                              请妥善保管\n");
        } else {
            scriptBuilderNeck.append("顾客存根                              请妥善保管\n");
        }
        scriptBuilderNeck.append("-------------------------------------------------\n");

        StringBuilder scriptBuilder = new StringBuilder();
        scriptBuilder.append("商户名称:").append(transInfo.getMerchantName()).append("\n");
        scriptBuilder.append("商户编号:").append(transInfo.getMerchantId()).append("\n");
        scriptBuilder.append("终端编号:").append(transInfo.getPosId()).append("\n");
        String payTypeText = AppConstants.getPayTypeText(transInfo.getChannelId());
        scriptBuilder.append("交易渠道:").append(payTypeText).append("\n");
        scriptBuilder.append("交易类型:").append(AppConstants.transType.get(transInfo.getState())).append("\n");
        scriptBuilder.append("交易时间:").append(transInfo.getPayTime()).append("\n");
        if (isCoupon) {
            scriptBuilder.append("订单金额:").append(moneyCount.getMoney()).append("\n");
            scriptBuilder.append("优惠金额:").append(moneyCount.getCoupon_money()).append("\n");
        }
        scriptBuilder.append("支付金额:\n");

        StringBuilder scriptBuilderAed = new StringBuilder();
        if (isRepeat) {
            scriptBuilderAed.append("备注:重打印 \n");
        }
        scriptBuilderAed.append("交易状态:").append(AppConstants.orderState.get(transInfo.getState())).append("\n");
        scriptBuilderAed.append("订单号:").append(transInfo.getOrderId()).append("\n\n");


        JSONObject jsonTitle = new JSONObject();//标题
        jsonTitle.put("content-type", "txt");
        jsonTitle.put("content", "通莞金服");
        jsonTitle.put("size", "3");
        jsonTitle.put("position", "center");
        jsonTitle.put("offset", "0");
        jsonTitle.put("bold", "0");
        jsonTitle.put("italic", "0");
        jsonTitle.put("height", "-1");

        JSONObject jsonNeck = new JSONObject();//
        jsonNeck.put("content-type", "txt");
        jsonNeck.put("content", scriptBuilderNeck.toString());
        jsonNeck.put("size", "1");
        jsonNeck.put("position", "left");
        jsonNeck.put("offset", "0");
        jsonNeck.put("bold", "0");
        jsonNeck.put("italic", "0");
        jsonNeck.put("height", "-1");

        JSONObject jsonContent = new JSONObject();//
        jsonContent.put("content-type", "txt");
        jsonContent.put("content", scriptBuilder.toString());
        jsonContent.put("size", "2");
        jsonContent.put("position", "left");
        jsonContent.put("offset", "0");
        jsonContent.put("bold", "0");
        jsonContent.put("italic", "0");
        jsonContent.put("height", "-1");

        JSONObject jsonMoney = new JSONObject();//
        jsonMoney.put("content-type", "txt");
        jsonMoney.put("content", "     RMB " + moneyCount.getAmount() + "\n");
        jsonMoney.put("size", "3");
        jsonMoney.put("position", "left");
        jsonMoney.put("offset", "0");
        jsonMoney.put("bold", "0");
        jsonMoney.put("italic", "0");
        jsonMoney.put("height", "-1");

        JSONObject jsonAed = new JSONObject();//
        jsonAed.put("content-type", "txt");
        jsonAed.put("content", scriptBuilderAed.toString());
        jsonAed.put("size", "2");
        jsonAed.put("position", "left");
        jsonAed.put("offset", "0");
        jsonAed.put("bold", "0");
        jsonAed.put("italic", "0");
        jsonAed.put("height", "-1");

        // 添加一维码打印
        JSONObject jsonDimension = new JSONObject();
        jsonDimension.put("content-type", "one-dimension");
        jsonDimension.put("content", transInfo.getOrderId());
        jsonDimension.put("size", "3");
        jsonDimension.put("position", "center");
        jsonDimension.put("height", "1");

        JSONObject jsonEnd = new JSONObject();//
        jsonEnd.put("content-type", "txt");
        jsonEnd.put("content", "微支付服务商:通莞金服\n\n\n");
        jsonEnd.put("size", "2");
        jsonEnd.put("position", "left");
        jsonEnd.put("offset", "0");
        jsonEnd.put("bold", "0");
        jsonEnd.put("italic", "0");
        jsonEnd.put("height", "-1");

        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonTitle);
        jsonArray.put(jsonNeck);
        jsonArray.put(jsonContent);
        jsonArray.put(jsonMoney);
        jsonArray.put(jsonAed);
        jsonArray.put(jsonDimension);
        jsonArray.put(jsonEnd);

        return new JSONObject().put("spos", jsonArray).toString();
    }

    /**
     * 重打印订单
     */
    public static String printRepeatOrder(PrintRepeat transInfo, int printUser, boolean isCoupon, @NonNull MoneyCount moneyCount) throws Exception {
        StringBuilder scriptBuilderNeck = new StringBuilder();
        scriptBuilderNeck.append("------------------------------------------------ \n");
        if (printUser == 0) {
            scriptBuilderNeck.append("商户存根                              请妥善保管\n");
        } else {
            scriptBuilderNeck.append("顾客存根                              请妥善保管\n");
        }
        scriptBuilderNeck.append("-------------------------------------------------\n");

        StringBuilder scriptBuilder = new StringBuilder();
        scriptBuilder.append("商户名称:").append(transInfo.getMerchantName()).append("\n");
        scriptBuilder.append("商户编号:").append(transInfo.getMerchantId()).append("\n");
        scriptBuilder.append("终端编号:").append(transInfo.getPosId()).append("\n");
        String payTypeText = AppConstants.getPayTypeText(transInfo.getChannelId());
        scriptBuilder.append("交易渠道:").append(payTypeText).append("\n");
        scriptBuilder.append("交易类型:").append(AppConstants.transType.get(transInfo.getState())).append("\n");
        scriptBuilder.append("交易时间:").append(transInfo.getPayTime()).append("\n");
        if (isCoupon) {
            scriptBuilder.append("订单金额:").append(moneyCount.getMoney()).append("\n");
            scriptBuilder.append("优惠金额:").append(moneyCount.getCoupon_money()).append("\n");
        }
        scriptBuilder.append("支付金额:\n");

        StringBuilder scriptBuilderAed = new StringBuilder();
        scriptBuilderAed.append("备注:重打印 \n");
        scriptBuilderAed.append("交易状态:").append(AppConstants.orderState.get(transInfo.getState())).append("\n");
        scriptBuilderAed.append("订单号:").append(transInfo.getOrderId()).append("\n\n");


        JSONObject jsonTitle = new JSONObject();//标题
        jsonTitle.put("content-type", "txt");
        jsonTitle.put("content", "通莞金服");
        jsonTitle.put("size", "3");
        jsonTitle.put("position", "center");
        jsonTitle.put("offset", "0");
        jsonTitle.put("bold", "0");
        jsonTitle.put("italic", "0");
        jsonTitle.put("height", "-1");

        JSONObject jsonNeck = new JSONObject();//
        jsonNeck.put("content-type", "txt");
        jsonNeck.put("content", scriptBuilderNeck.toString());
        jsonNeck.put("size", "1");
        jsonNeck.put("position", "left");
        jsonNeck.put("offset", "0");
        jsonNeck.put("bold", "0");
        jsonNeck.put("italic", "0");
        jsonNeck.put("height", "-1");

        JSONObject jsonContent = new JSONObject();//
        jsonContent.put("content-type", "txt");
        jsonContent.put("content", scriptBuilder.toString());
        jsonContent.put("size", "2");
        jsonContent.put("position", "left");
        jsonContent.put("offset", "0");
        jsonContent.put("bold", "0");
        jsonContent.put("italic", "0");
        jsonContent.put("height", "-1");

        JSONObject jsonMoney = new JSONObject();//
        jsonMoney.put("content-type", "txt");
        jsonMoney.put("content", "     RMB " + moneyCount.getAmount() + "\n");
        jsonMoney.put("size", "3");
        jsonMoney.put("position", "left");
        jsonMoney.put("offset", "0");
        jsonMoney.put("bold", "0");
        jsonMoney.put("italic", "0");
        jsonMoney.put("height", "-1");

        JSONObject jsonAed = new JSONObject();//
        jsonAed.put("content-type", "txt");
        jsonAed.put("content", scriptBuilderAed.toString());
        jsonAed.put("size", "2");
        jsonAed.put("position", "left");
        jsonAed.put("offset", "0");
        jsonAed.put("bold", "0");
        jsonAed.put("italic", "0");
        jsonAed.put("height", "-1");

        // 添加一维码打印
        JSONObject jsonDimension = new JSONObject();
        jsonDimension.put("content-type", "one-dimension");
        jsonDimension.put("content", transInfo.getOrderId());
        jsonDimension.put("size", "3");
        jsonDimension.put("position", "center");
        jsonDimension.put("height", "1");

        JSONObject jsonEnd = new JSONObject();//
        jsonEnd.put("content-type", "txt");
        jsonEnd.put("content", "微支付服务商:通莞金服\n\n\n");
        jsonEnd.put("size", "2");
        jsonEnd.put("position", "left");
        jsonEnd.put("offset", "0");
        jsonEnd.put("bold", "0");
        jsonEnd.put("italic", "0");
        jsonEnd.put("height", "-1");

        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonTitle);
        jsonArray.put(jsonNeck);
        jsonArray.put(jsonContent);
        jsonArray.put(jsonMoney);
        jsonArray.put(jsonAed);
        jsonArray.put(jsonDimension);
        jsonArray.put(jsonEnd);

        return new JSONObject().put("spos", jsonArray).toString();
    }

    /**
     * 打印结算总计
     */
    public static String printSettleTotal(List<Total> mTotals, String lastTime, String thisTime, boolean isRepeat) throws Exception {
        if (mTotals.size() <= 0) {
            return null;
        }
        int length = mTotals.size() - 1;
        String content;
        if (isRepeat) {
            content = "--------------------------------\n" +// 打印虚线
                    "商户名称:" + User.getInstance().getMerchantName() + "\n" +
                    "商户编号:" + User.getInstance().getMerchantId() + "\n" +
                    "终端编号:" + User.getInstance().getPosId() + "\n" +
                    "上次结算时间：\n" +
                    "  " + lastTime + "\n" +
                    "本次结算时间：\n" +
                    "  " + thisTime + "\n" +
                    "备注：重打印" + "\n" +
                    "--------------------------------\n" +// 打印虚线
                    "总计：\n" +
                    "成功交易：" + mTotals.get(length).getSuccessNum() + "笔，共计" + mTotals.get(length).getSuccessMoney() + "元" + "\n" +
                    "--------------------------------\n" +// 打印虚线
                    "支付类型总计：\n";
        } else {
            content = "--------------------------------\n" +// 打印虚线
                    "商户名称:" + User.getInstance().getMerchantName() + "\n" +
                    "商户编号:" + User.getInstance().getMerchantId() + "\n" +
                    "终端编号:" + User.getInstance().getPosId() + "\n" +
                    "上次结算时间：\n" +
                    "  " + lastTime + "\n" +
                    "本次结算时间：\n" +
                    "  " + thisTime + "\n" +
                    "--------------------------------\n" +// 打印虚线
                    "总计：\n" +
                    "成功交易：" + mTotals.get(length).getSuccessNum() + "笔，共计" + mTotals.get(length).getSuccessMoney() + "元" + "\n" +
                    "--------------------------------\n" +// 打印虚线
                    "支付类型总计：\n";
        }

        StringBuilder scriptBuilder = new StringBuilder();//printText 一般不超过15行 或者106个字符
        for (Total total : mTotals) {
            String type = total.getType();
            if ("wx".equals(type)) {
                scriptBuilder.append("微信支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("zfb".equals(type)) {
                scriptBuilder.append("支付宝支付      消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("yzf".equals(type)) {
                scriptBuilder.append("翼支付          消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("xj".equals(type)) {
                scriptBuilder.append("现金支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("hyc".equals(type)) {
                scriptBuilder.append("会员卡支付      消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("qq".equals(type)) {
                scriptBuilder.append("QQ钱包          消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("pos".equals(type)) {
                scriptBuilder.append("刷卡支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("jf".equals(type)) {
                scriptBuilder.append("积分支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("dk".equals(type)) {
                scriptBuilder.append("代扣支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("xl".equals(type)) {
                scriptBuilder.append("外卡支付        消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            } else if ("ylsm".equals(type)) {
                scriptBuilder.append("银联钱包支付    消费\n");
                scriptBuilder.append("订单数：	").append(total.getSuccessNum()).append("\n");
                scriptBuilder.append("总金额：	").append(total.getSuccessMoney()).append("\n");
            }
        }

        JSONObject jsonTitle = new JSONObject();//标题
        jsonTitle.put("content-type", "txt");
        jsonTitle.put("content", "结算总计");
        jsonTitle.put("size", "3");
        jsonTitle.put("position", "center");
        jsonTitle.put("offset", "0");
        jsonTitle.put("bold", "0");
        jsonTitle.put("italic", "0");
        jsonTitle.put("height", "-1");

        JSONObject jsonContent = new JSONObject();//
        jsonContent.put("content-type", "txt");
        jsonContent.put("content", content);
        jsonContent.put("size", "2");
        jsonContent.put("position", "left");
        jsonContent.put("offset", "0");
        jsonContent.put("bold", "0");
        jsonContent.put("italic", "0");
        jsonContent.put("height", "-1");

        JSONObject jsonContent2 = new JSONObject();//
        jsonContent2.put("content-type", "txt");
        jsonContent2.put("content", scriptBuilder.toString() + "\n\n\n");
        jsonContent2.put("size", "2");
        jsonContent2.put("position", "left");
        jsonContent2.put("offset", "0");
        jsonContent2.put("bold", "0");
        jsonContent2.put("italic", "0");
        jsonContent2.put("height", "-1");

        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonTitle);
        jsonArray.put(jsonContent);
        jsonArray.put(jsonContent2);
        return new JSONObject().put("spos", jsonArray).toString();
    }

    /*打印结算明细*/
    public static String printSettleOrder(List<SettleOrder> mSettleOrders) throws Exception {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonTitle = new JSONObject();//标题
        jsonTitle.put("content-type", "txt");
        jsonTitle.put("content", "结算明细");
        jsonTitle.put("size", "3");
        jsonTitle.put("position", "center");
        jsonTitle.put("offset", "0");
        jsonTitle.put("bold", "0");
        jsonTitle.put("italic", "0");
        jsonTitle.put("height", "-1");

        jsonArray.put(jsonTitle);

        List<List<SettleOrder>> ret = Utils.split(mSettleOrders, 30);
        for (List<SettleOrder> settle : ret) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("\n");
            for (SettleOrder bean : settle) {
                stringBuilder.append(AppConstants.getPayTypeText(bean.getChannelId())).append("   ").append(bean.getOrderId()).append("  ").append(bean.getPayMoney()).append("\n");
            }
            JSONObject jsonContent = new JSONObject();//
            jsonContent.put("content-type", "txt");
            jsonContent.put("content", stringBuilder.toString() + "\n\n\n\n\n");
            jsonContent.put("size", "1");
            jsonContent.put("position", "center");
            jsonContent.put("offset", "0");
            jsonContent.put("bold", "0");
            jsonContent.put("italic", "0");
            jsonContent.put("height", "-1");
            jsonArray.put(jsonContent);
        }
        return new JSONObject().put("spos", jsonArray).toString();
    }


}
